<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>

      <el-form-item label="KEY" prop="obj_key">
        <el-input v-model="ruleForm.obj_key" style="width: 90%;float: left;"></el-input>
        <div style="width: 10%;float: right;">
          <el-button type="primary" @click="getRandomString()">获取</el-button>
        </div>
      </el-form-item>

      <el-form-item label="所属项目" prop="project">
        <el-select
          style="width: 100%;"
          v-model="optionsProjectId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入项目名称"
          :remote-method="getProjectInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in project_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属模块" prop="module">
        <el-select
          style="width: 100%;"
          :disabled="!optionsProjectId.length"
          v-model="optionsModuleId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getModuleInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in module_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="步骤模板" prop="into_sql_push">
        <el-input v-model="ruleForm.steps_data" type="textarea" :autosize="{ minRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
  export default {
    name: 'EditSteps',
    data() {
      return {
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        projects: [], // 项目列表，获取1000条项目数据
        EditUrl: this.$http.StepsURL,  // get编辑的URL
        EditIdStr: 'steps_id',  // 模块id字符串
        pushObjName: 'Steps',  // 成功后跳转

        // 其它表数据,搜索到的全部数据
        project_info: [], // 项目列表，获取1000条项目数据
        module_info: [], // 模块列表，获取1000条项目数据

        // 编辑和添加选中的数据id
        loading: false,
        optionsProjectId: [],
        optionsModuleId: [],

        ruleForm: {
          title: '',
          steps_data:'',  // 步骤模板
          project: '',
          module: '',
          obj_key:'',
          project_info: '',
          module_info: '',
          status: '',
        },  //
        rules: {
          title: [
            {required: true, message: '请输入标题', trigger: 'blur'},
            {min: 1, max: 64, message: '长度在 1 到 64 个字符', trigger: 'blur'}
          ],
        }
      };
    },
    created() {
      this.getEditData();   // get回将要编辑的数据
      this.optionsCommon = this.getChoices.common; // 获取标准CHOICES
      this.$myTool.getObjList(this, this.$http.projectURL, this.projects);  // 获取项目1000条数据
    },
    methods: {
      submitForm() {
        this.$myTool.submitForm(this)
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this);
      },

      // 获取随机字符串
      getRandomString() {
        let queryDict = {
          str_len: 16
        };
        this.$myTool.getRandomString(this,queryDict)
      },

      // 项目信息搜索
      getProjectInfo(query) {
        this.$myTool.getObjList(this, this.$http.projectURL, this.project_info, query)
      },

      getModuleInfo(query) {
        let quertDict = {
          inputObjName: query,
          module_project: this.optionsProjectId.length ? this.optionsProjectId[0] : null
        };
        this.$myTool.getObjList(this, this.$http.moduleURL, this.module_info, quertDict)
      },

      // get将要编辑的数据
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
    // 监听
    watch: {
      'ruleForm.project_info': function (value) {
        if (value) {
          this.optionsProjectId.push(value.id);
          this.project_info.push(value);
        } else {
          this.optionsProjectId = [];
          this.project_info = [];
        }
      },
      optionsProjectId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.project = value[0]
      },
      'ruleForm.module_info': function (value) {
        if (value) {
          this.optionsModuleId.push(value.id);
          this.module_info.push(value);
        } else {
          this.optionsModuleId = [];
          this.module_info = [];
        }
      },
      optionsModuleId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.module = value[0]
      },
    },
    // 计算属性
    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },

  }
</script>
